package kr.hkit.powerjava.chapter14.work;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MilePanel extends JPanel implements ActionListener {
	private JTextField inputField;
	private JTextField resultField;
	public static final double MILE = 1.609344;
	
	public MilePanel() {
		initialize();
	}
	private void initialize() {
		JLabel lbl = new JLabel("거리를 마일 단위로 입력하세요");
		add(lbl);
		
		inputField = new JTextField();
		add(inputField);
		inputField.setColumns(5);
		
		JButton btn01 = new JButton("변환");
		btn01.addActionListener(this);
		add(btn01);
		
		resultField = new JTextField();
		add(resultField);
		resultField.setColumns(20);
		
		setVisible(true);
	}
	
	public String getInput(){
		return inputField.getText();
	}
	public void setResultField(String text) {
		this.resultField.setText(text);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("변환")){
			double result = Double.parseDouble(getInput()) * MILE;
			String sr = Double.toString(result);
			setResultField(sr);
		}
	}
}
